#include "Precompile.h"
#include "StateManager.h"

StateManager::StateManager(){

}

void StateManager::addState(IStatePtr state){
	std::set<IStatePtr>::iterator itr = m_stateStack.find(state);
	if(itr != m_stateStack.end()){
		LOG_ERROR("Adding an already existing state, is this intended?");
		assert(0);
		
		// Remove old one first.
		(*itr)->onRemoved();
		m_stateStack.erase(itr);
	}

	state->onAdded();
	m_stateStack.insert(state);
}

void StateManager::removeState(IStatePtr state){
	std::set<IStatePtr>::iterator itr = m_stateStack.find(state);
	if(itr == m_stateStack.end()){
		LOG_ERROR("Removing an unexisting state.");
		assert(0);
		return;
	}

	m_stateStack.erase(state);
}

void StateManager::update(){
	std::set<IStatePtr>::iterator itr = m_stateStack.begin();
	for(;itr != m_stateStack.end();itr++){
		(*itr)->onUpdated();
	}
}